<?php
namespace App\Models;

class HdLog extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "hd_mo_log";
    }

    private function t() {
        return 'App\Models\HdLog';
    }

    /**
     * 统计短信总数
     *
     * @param string $act 端口
     * @param array  $where 搜索条件
     *
     * @return int|false
     */
    public function total($act, $where) {
        $query = $this->select('COUNT(*) AS cnt')->from($this->t());
        $query->andWhere('long_code=:long_code:', ['long_code' => $act]);
        if(is_array($where) && count($where) > 0) {
            if(isset($where['mobile'])) {
                $query->andWhere('mobile=:mobile:', ['mobile' => $where['mobile']]);
            }
            if(isset($where['mt_status']) && $where['mt_status'] == 1) {
                $query->andWhere('mt_status=:mt_status:', ['mt_status' => $where['mt_status']]);
            }
            if(isset($where['mt_status']) && $where['mt_status'] == -1) {
                $query->andWhere('mt_status!=:mt_status:', ['mt_status' => 1]);
            }
            if(isset($where['start_date'])) {
                $query->andWhere('create_at>=:start:', ['start' => date("Y-m-d H:i:s", strtotime($where['start_date']))]);
            }
            if(isset($where['end_date'])) {
                $query->andWhere('create_at<:end:', ['end' => date("Y-m-d H:i:s", strtotime($where['end_date']) + 24 * 60 * 60)]);
            }
        }
        try {
            $ret = $query->getQuery()->execute();
            if($ret != false) {
                $ret = $ret->toArray();
                if(is_array($ret) && count($ret) > 0 && isset($ret[0]['cnt'])) {
                    return intval($ret[0]['cnt']);
                }
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取短信信息
     *
     * @param int    $offset 起始位置
     * @param string $act 端口
     * @param int    $limit 限制条数
     * @param array  $where 搜索条件
     *
     * @return array|false
     */
    public function gets($where, $act, $offset, $limit) {
        $query = $this->select(['id', 'mobile', 'message', 'mt_api_message', 'mt_status', 'create_at'])->from($this->t());
        $query->andWhere('long_code=:long_code:', ['long_code' => $act]);
        if(is_array($where) && count($where) > 0) {
            if(isset($where['mobile'])) {
                $query->andWhere('mobile=:mobile:', ['mobile' => $where['mobile']]);
            }
            if(isset($where['mt_status']) && $where['mt_status'] == 1) {
                $query->andWhere('mt_status=:mt_status:', ['mt_status' => $where['mt_status']]);
            }
            if(isset($where['mt_status']) && $where['mt_status'] == -1) {
                $query->andWhere('mt_status!=:mt_status:', ['mt_status' => 1]);
            }
            if(isset($where['start_date'])) {
                $query->andWhere('create_at>=:start:', ['start' => date("Y-m-d H:i:s", strtotime($where['start_date']))]);
            }
            if(isset($where['end_date'])) {
                $query->andWhere('create_at<:end:', ['end' => date("Y-m-d H:i:s", strtotime($where['end_date']) + 24 * 60 * 60)]);
            }
        }
        try {
            $ret = $query->orderBy('create_at DESC')->limit($limit, $offset)->getQuery()->execute();
            if($ret !== false) {
                return $ret->toArray();
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getsByMobile($mobile) {
        $query = $this->select(['id', 'mobile', 'message', 'mt_api_message','long_code', 'mt_status', 'create_at'])->from($this->t());
        $query->andWhere('mobile=:mobile:', ['mobile' => $mobile]);
        try {
            $ret = $query->getQuery()->execute();
            if($ret !== false) {
                return $ret->toArray();
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取互动短信信息
     *
     * @param string $action 短信类型
     * @param array  $where 搜索条件
     *
     * @return array|false
     */
    public function getsByDownload($where, $action) {
        $query = $this->select(['id', 'mobile', 'message', 'mt_api_message', 'mt_status', 'create_at'])->from($this->t());
        $query->andWhere('long_code=:long_code:', ['long_code' => $action]);
        if(is_array($where) && count($where) > 0) {
            if(isset($where['mobile'])) {
                $query->andWhere('mobile=:mobile:', ['mobile' => $where['mobile']]);
            }
            if(isset($where['mt_status']) && $where['mt_status'] == 1) {
                $query->andWhere('mt_status=:mt_status:', ['mt_status' => $where['mt_status']]);
            }
            if(isset($where['mt_status']) && $where['mt_status'] == -1) {
                $query->andWhere('mt_status!=:mt_status:', ['mt_status' => 1]);
            }
            if(isset($where['start_date'])) {
                $query->andWhere('create_at>=:start:', ['start' => date("Y-m-d H:i:s", strtotime($where['start_date']))]);
            }
            if(isset($where['end_date'])) {
                $query->andWhere('create_at<:end:', ['end' => date("Y-m-d H:i:s", strtotime($where['end_date']) + 24 * 60 * 60)]);
            }
        }
        try {
            $ret = $query->getQuery()->execute();
            if($ret !== false) {
                return $ret->toArray();
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 根据id值获取短信信息
     *
     * @param int $id 短信id值
     *
     * @return array|false
     */
    public function get($id) {
        if($id == '' || !is_numeric($id)) {
            return false;
        }
        //@formatter:off
        $ret = $this->select('*')
            ->from($this->t())
            ->where('id =:id:', array('id' => $id))
            ->limit(1)
            ->getQuery()
            ->execute();
        //@formatter:on
        if($ret !== false) {
            $ret = $ret->toArray();
            if(count($ret) > 0) {
                return $ret[0];
            }
        }
        return false;
    }
}

